<?php

namespace app\admin\controller;

use think\cache\driver\File;
use think\Controller;
use think\Request;
use think\Db;
use think\Validate;

//use think\Paginator;

//分页功能
use think\Paginator;
use think\Paginator\driver\Bootstrap;

class Teacher extends AdminController
{
    //展示页面
    public function index()
    {
//echo 111;die;
        $res = Db::name('teacher')
            ->paginate(3, false, ['query' => request()->param()]);
            // var_dump($res);die;
        
      $data = Db::name('teacher')
                ->order('id desc')
                ->paginate(3);
     $a = [];           
    foreach ($data as $v) {
            $d = Db::name('teasort')
                   ->field('teasort')
                   ->where('id',$v['teasid'])
                   ->find();
            $e = array_push($v,$d['teasort']);
             $a[] = $v;
        }
        $page=$res->render();
        return view('', ['teacher' => $a,'page'=>$page]);
    }
    
    public function select()
    {   

        $teasort = input('teasort');
        
        if(empty($teasort)){
             $this->error('请填写正确的搜索内容', url('admin/teacher/index'));
        }
        $res =Db::name('teasort')
                ->field('id')
                ->where('teasort','like','%'.$teasort.'%')
                ->find();
        // dump($res);die;
        $data = Db::name('teacher')
                  ->where('teasid',$res['id'])
                  ->paginate(5, false, ['query' => request()->param()]);
    $a = [];           
    foreach ($data as $v) {
            $d = Db::name('teasort')
                   ->field('teasort')
                   ->where('id',$v['teasid'])
                   ->find();
            $e = array_push($v,$d['teasort']);
             $a[] = $v;
        }
    // dump($a);die;
        $page=$data->render();
        return view('teacher/index', ['teacher' => $a,'page'=>$page]);      
    }

    public function add()
    { 
          $t=Db::field('teasort')
             ->name('teasort')
             ->select();
        $e=[];
       foreach ($t as $key => $j) {
         foreach ($j as $k => $s) {
           $e[] = $s;
         }
      }

        return View('',[
            'e' =>$e
             ]);
    }

    public function edit()
    {
        $id = input('id');
        $user = Db::name('teacher')
            ->where('id', $id)
            ->find();
        $b = Db::name('teasort')
            ->field('teasort')
            ->where('id',$user['teasid'])
            ->find();
        $user['teasort']=$b['teasort'];
        // var_dump($b);die;
        return view('admin/teacher/index', ['user_info' => $user]);
    }

    public function doEdit()
    {   

        $uid = input('id');
        $file = request()->file('img');
        if ($file) {
            $info = $file->validate(['size' => 1005678, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if (!$info) {
                $err = $file->getError();
                $this->error($err, url('admin/teacher/index'));
            } else {
                $img = $info->getSaveName();
            }
        }
         $a=['teasort'=>input('teasort')];
        
         $b = Db::name('teasort')
                ->field('id')
                ->where('teasort',$a['teasort'])
                ->find();
         if(empty($b)){
            $this->error('没有该分类');
         }

        if (isset($img)) {
            $data = [
                'tname' => input('teachername'),
                'icon' => $img,
                'glory' => input('glory'),
                'desc' => input('introduction'),
                'teasid'=>$b['id']
            ];
        } else {
            $data = [
                'tname' => input('teachername'),
                'glory' => input('glory'),
                'desc' => input('introduction'),
                'teasid'=>$b['id']
            ];
        }
         
        $res = Db::name('teacher')
            ->where('id', $uid)
            ->update($data);
        if ($res > 0) {
            $this->success('编辑成功', url('admin/teacher/index'));
        } else if ($res == 0) {
            $this->error('你没有做任何修改', url('admin/teacher/index'));
        }
    }

    public function doAdd()
    {
        $file = request()->file('img');
        if ($file) {
            $info = $file->validate(['size' => 1005678, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if (!$info) {
                $err = $file->getError();
                $this->error($err, url('admin/teacher/index'));
            } else {
                $img = $info->getSaveName();
            }
        }

         //验证
       $rule = [
          'tname' => 'require|max:25|chsDash',
          'glory'=>'require|max:25|chsDash',
          'desc' => 'require|max:200|chsDash',
         
      ];
          $msg = [
      'tname.require' => '教师的名字不能为空',
      'tname.max' => '教师的名称不能过长',
      'tname.chsDash'=>'教师的名称只能是汉字、字母、数字和下划线_及破折号-',
       'glory.require'=>'教师的称号不能为空',
       'glory.max' => '教师的称号过长',
       'glory.chsDash'=>'教师的称号只能是汉字、字母、数字和下划线_及破折号-',
       'desc.require'=>'教师的描述不能为空',
       'desc.max'=>'教师的描述不能为空',
       'desc.chsDash'=>'教师的描述只能是汉字、字母、数字和下划线_及破折号-'
      ];


         $a=['teasort'=>input('teasort')];
         $b = Db::name('teasort')
                ->field('id')
                ->where('teasort',$a['teasort'])
                ->find();
         if(empty($b)){
            $this->error('没有该分类');
         }

        if (isset($img)) {
            $data = [
                'tname' => input('teachername'),
                'icon' => $img,
                'glory' => input('glory'),
                'desc' => input('introduction'),
                'teasid'=>$b['id']
            ];
        } else {
            $data = [
                'tname' => input('teachername'),
                'glory' => input('glory'),
                'desc' => input('introduction'),
                'teasid'=>$b['id']
            ];
        }
        $validate = new Validate($rule,$msg);
      if(!$validate->check($data)){
      $this->error($validate->getError());
      }
        $res = Db::name('teacher')
            ->insert($data);
        if ($res > 0) {
            $this->success('添加成功', url('admin/teacher/index'));
        }
    }

    public function dodel()
    {
        $id = input('id');
        $res = Db::name('teacher')
            ->where('id', $id)
            ->delete();
        return $res;
    }
}
